#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtCore/qmath.h>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton7_clicked()
{
    this->ui->textEdit->insertPlainText("7");
    ui->textEdit->setFocus();
}

void MainWindow::on_pushButton8_clicked()
{
    this->ui->textEdit->insertPlainText("8");
    ui->textEdit->setFocus();
}

void MainWindow::on_pushButton9_clicked()
{
    this->ui->textEdit->insertPlainText("9");
    ui->textEdit->setFocus();
}

void MainWindow::on_pushButton4_clicked()
{
    this->ui->textEdit->insertPlainText("4");
    ui->textEdit->setFocus();
}

void MainWindow::on_pushButton5_clicked()
{
    this->ui->textEdit->insertPlainText("5");
    ui->textEdit->setFocus();
}

void MainWindow::on_pushButton6_clicked()
{
    this->ui->textEdit->insertPlainText("6");
    ui->textEdit->setFocus();
}

void MainWindow::on_pushButton1_clicked()
{
    this->ui->textEdit->insertPlainText("1");
    ui->textEdit->setFocus();
}

void MainWindow::on_pushButton2_clicked()
{
    this->ui->textEdit->insertPlainText("2");
    ui->textEdit->setFocus();
}

void MainWindow::on_pushButton3_clicked()
{
    this->ui->textEdit->insertPlainText("3");
    ui->textEdit->setFocus();
}

void MainWindow::on_pushButton0_clicked()
{
    this->ui->textEdit->insertPlainText("0");
    ui->textEdit->setFocus();
}

void MainWindow::on_pushButton_clear_clicked()
{
    this->ui->textEdit->clear();
}
void MainWindow::on_pushButton_dot_clicked()
{
    QString str=this->ui->textEdit->toPlainText();
    int index=str.indexOf("\n");
    if(index!=-1)
    {
        str.remove(0,index+1);
    }
    if(!str.contains("."))
        this->isDot=false;
    if(!this->isDot)
    {
        this->ui->textEdit->insertPlainText(".");
        this->isDot=true;
        this->ui->textEdit->setFocus();
    }
}

void MainWindow::on_pushButton_delete_clicked()
{
    QTextCursor tc=this->ui->textEdit->textCursor();
    tc.deletePreviousChar();
}

void MainWindow::on_pushButton_negative_clicked()
{
    QString str=this->ui->textEdit->toPlainText();
    QTextCursor cursor = ui->textEdit->textCursor();
    QStringList strlist;
    int index=str.indexOf("\n");
    if(index!=-1)
    {
        strlist=str.split("\n");
        str=strlist[1];
    }
    if(!str.isEmpty())
    {
        if(str.at(0)=="-")
           str.remove('-');
        else
           str.prepend("-");
        if(index!=-1)
        this->ui->textEdit->setPlainText(strlist[0]+"\n"+str);
        else
        this->ui->textEdit->setPlainText(str);
        cursor.movePosition(QTextCursor::End);
        ui->textEdit->setTextCursor(cursor);
    }
}

void MainWindow::on_pushButton_inv_clicked()
{
    QString str=this->ui->textEdit->toPlainText();
    QTextCursor cursor = ui->textEdit->textCursor();
    QStringList strlist;
    int index=str.indexOf("\n");
    if(index!=-1)
    {
        strlist=str.split("\n");
        str=strlist[1];
    }
    double num=str.toDouble();
    num=1/num;
    str=QString::number(num);
    if(index!=-1)
    this->ui->textEdit->setPlainText(strlist[0]+"\n"+str);
    else
    this->ui->textEdit->setPlainText(str);
    cursor.movePosition(QTextCursor::End);
    ui->textEdit->setTextCursor(cursor);
}

void MainWindow::on_pushButton_per_clicked()
{
    QString str=this->ui->textEdit->toPlainText();
    QTextCursor cursor = ui->textEdit->textCursor();
    QStringList strlist;
    int index=str.indexOf("\n");
    double num0,num;
    if(index!=-1)
    {
        strlist=str.split("\n");
        strlist[0].chop(1);//chop the operator
        num0=strlist[0].toDouble();
        num=strlist[1].toDouble();
        if(!strlist[1].isEmpty() && (this->c=='+' || this->c=='-'))
        num=num*num0/100;
        else if(!strlist[1].isEmpty() && (this->c=='*' || this->c=='/' || this->c=='^'))
        num=num/100;
        str=QString::number(num);
        this->ui->textEdit->setPlainText(strlist[0]+this->c+"\n"+str);
    }
    else
    {
        num=str.toDouble();
        num=num/100;
        str=QString::number(num);
        this->ui->textEdit->setPlainText(str);
    }
    cursor.movePosition(QTextCursor::End);
    ui->textEdit->setTextCursor(cursor);
}

void MainWindow::on_pushButton_ln_clicked()
{
    QString str=this->ui->textEdit->toPlainText();
    QTextCursor cursor = ui->textEdit->textCursor();
    QStringList strlist;
    int index=str.indexOf("\n");
    if(index!=-1)
    {
        strlist=str.split("\n");
        str=strlist[1];
    }
    double num=str.toDouble();
    num=qLn(num);
    str=QString::number(num);
    if(index!=-1)
    this->ui->textEdit->setPlainText(strlist[0]+"\n"+str);
    else
    this->ui->textEdit->setPlainText(str);
    cursor.movePosition(QTextCursor::End);
    ui->textEdit->setTextCursor(cursor);
}

void MainWindow::on_pushButton_plus_clicked()
{
    QString str=this->ui->textEdit->toPlainText();
    QTextCursor cursor = ui->textEdit->textCursor();
    int index=str.indexOf("\n");
    if(index!=-1)
    {
        MainWindow::on_pushButton_equal_clicked();
        this->ui->textEdit->insertPlainText("+\n");
        this->a=this->eqn;
        this->c='+';
    }
    else
    {
        this->a=str.toDouble();
        this->c='+';
        this->ui->textEdit->insertPlainText("+\n");
    }
}

void MainWindow::on_pushButton_equal_clicked()
{
    QString str=this->ui->textEdit->toPlainText();
    QTextCursor cursor = ui->textEdit->textCursor();
    int index=str.indexOf("\n");
    if(index!=-1)
    {
        str.remove(0,index+1);
        this->b=str.toDouble();
        switch (this->c)
        {
        case '+':
            this->eqn=this->a+this->b;
            break;
        case '-':
            this->eqn=this->a-this->b;
            break;
        case '*':
            this->eqn=this->a*this->b;
            break;
        case '/':
            this->eqn=this->a/this->b;
            break;
        case '^':
            this->eqn=qPow(this->a,this->b);
            break;
        }
        str=QString::number(eqn);
        this->ui->textEdit->setPlainText(str);
        this->c=' ';
        cursor.movePosition(QTextCursor::End);
        ui->textEdit->setTextCursor(cursor);
    }
}

void MainWindow::on_pushButton_clearE_clicked()
{
    QString str=this->ui->textEdit->toPlainText();
    QTextCursor cursor = ui->textEdit->textCursor();
    int index=str.indexOf("\n");
    if(index!=-1)
    {
        str.remove(index,str.length());
        this->ui->textEdit->setPlainText(str+"\n");
        cursor.movePosition(QTextCursor::End);
        ui->textEdit->setTextCursor(cursor);
    }
    else
        this->ui->textEdit->clear();
}
void MainWindow::on_pushButton_divide_clicked()
{
    QString str=this->ui->textEdit->toPlainText();
    QTextCursor cursor = ui->textEdit->textCursor();
    int index=str.indexOf("\n");
    if(index!=-1)
    {
        MainWindow::on_pushButton_equal_clicked();
        this->ui->textEdit->insertPlainText("÷\n");
        this->a=this->eqn;
        this->c='/';
    }
    else
    {
        this->a=str.toDouble();
        this->c='/';
        this->ui->textEdit->insertPlainText("÷\n");
    }
}
void MainWindow::on_pushButton_minus_clicked()
{
    QString str=this->ui->textEdit->toPlainText();
    QTextCursor cursor = ui->textEdit->textCursor();
    int index=str.indexOf("\n");
    if(index!=-1)
    {
        MainWindow::on_pushButton_equal_clicked();
        this->ui->textEdit->insertPlainText("-\n");
        this->a=this->eqn;
        this->c='-';
    }
    else
    {
        this->a=str.toDouble();
        this->c='-';
        this->ui->textEdit->insertPlainText("-\n");
    }
}
void MainWindow::on_pushButton_times_clicked()
{
    QString str=this->ui->textEdit->toPlainText();
    QTextCursor cursor = ui->textEdit->textCursor();
    int index=str.indexOf("\n");
    if(index!=-1)
    {
        MainWindow::on_pushButton_equal_clicked();
        this->ui->textEdit->insertPlainText("*\n");
        this->a=this->eqn;
        this->c='*';
    }
    else
    {
        this->a=str.toDouble();
        this->c='*';
        this->ui->textEdit->insertPlainText("*\n");
    }
}
void MainWindow::on_pushButton_power_clicked()
{
    QString str=this->ui->textEdit->toPlainText();
    QTextCursor cursor = ui->textEdit->textCursor();
    int index=str.indexOf("\n");
    if(index!=-1)
    {
        MainWindow::on_pushButton_equal_clicked();
        this->ui->textEdit->insertPlainText("^\n");
        this->a=this->eqn;
        this->c='^';
    }
    else
    {
        this->a=str.toDouble();
        this->c='^';
        this->ui->textEdit->insertPlainText("^\n");
    }
}
